#include <filesystem>
#include <iostream>
#include <vector>
#include <fstream>
#include <random>
#include <algorithm>

#include "car.h"
#include "student.h"

#include <string>  // 添加string头文件

void saveToTxt(const std::vector<Car>& cars, const std::string& filename);
void loadFromTxt(std::vector<Car>& cars, const std::string& filename);
void showCars(const std::vector<Car>& cars);
std::string generateRandomStudentID();
void assignCars(std::vector<Car>& cars);

int main(int argc, char* argv[]) {  // 修改为带参数形式
    std::vector<Car> cars;
    std::vector<Student> students;
    std::vector<Car> loadedCars;
    bool debug = false;  // 默认关闭debug模式
    
    // 解析命令行参数
    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if (arg == "--debug" || arg == "-d") {
            debug = true;  // 只有指定参数时才启用debug模式
        }
    }

    // 一次性输入所有车辆基本信息
    auto inputAllCars = [](std::vector<Car>& cars) {
        std::cout << "请一次性输入10辆车的编号信息：" << std::endl;
        for (int i = 0; i < 10; ++i) {
            std::cout << "车辆" << i+1 << "：" << std::endl;
            Car car;
            car.inputBasicInfo(i); // 只输入车辆编号和底盘编号
            cars.push_back(car);
        }
    };

    if (std::filesystem::exists("cars.txt")) {
        loadFromTxt(loadedCars, "cars.txt");
        if (debug) {
            // Debug模式直接进入分配功能
            assignCars(loadedCars);
        } else if (loadedCars.size() < 10) {
            std::cout << "车辆信息不完整，需要重新录入" << std::endl;
            loadedCars.clear();
            inputAllCars(loadedCars);
            saveToTxt(loadedCars, "cars.txt");
        } else {
            // 正常流程进入分配功能
            assignCars(loadedCars);
        }
    } else {
        if (debug) {
            // Debug模式自动生成数据
            for (int i = 0; i < 10; ++i) {
                Car car;
                car.inputBasicInfo(i, true); // 自动生成信息
                loadedCars.push_back(car);
            }
            saveToTxt(loadedCars, "cars.txt");
            assignCars(loadedCars);
        } else {
            // 正常模式手动输入
            inputAllCars(loadedCars);
            saveToTxt(loadedCars, "cars.txt");
            assignCars(loadedCars);
        }
    }

    showCars(loadedCars);
    return 0;
}

std::string generateRandomStudentID() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(100, 999);
    return "E2022" + std::to_string(dis(gen));
}

void assignCars(std::vector<Car>& cars) {
    int currentIndex = 0;
    while (true) {
        std::cout << "\n当前车辆 (" << currentIndex+1 << "/10):" << std::endl;
        std::cout << "车辆编号: " << cars[currentIndex].id << std::endl;
        std::cout << "底盘编号: " << cars[currentIndex].chassis.id << std::endl;
        std::cout << "当前分配学生: " << (cars[currentIndex].assignedStudent.id.empty() ? "无" : cars[currentIndex].assignedStudent.id) << std::endl;
        
        std::cout << "\n操作选项:" << std::endl;
        std::cout << "1. 修改学生信息" << std::endl;
        std::cout << "n. 下一辆车" << std::endl;
        std::cout << "p. 上一辆车" << std::endl;
        std::cout << "q. 退出分配" << std::endl;
        std::cout << "请选择操作: ";
        
        char choice;
        std::cin >> choice;
        
        switch (choice) {
            case '1': {
                Student student;
                std::cout << "请输入学号: ";
                std::cin >> student.id;
                std::cout << "请输入姓名: ";
                std::cin >> student.name;
                cars[currentIndex].assignedStudent = student;
                saveToTxt(cars, "cars.txt");
                break;
            }
            case 'n':
                if (currentIndex < 9) currentIndex++;
                else std::cout << "已经是最后一辆车" << std::endl;
                break;
            case 'p':
                if (currentIndex > 0) currentIndex--;
                else std::cout << "已经是第一辆车" << std::endl;
                break;
            case 'q':
                return;
            default:
                std::cout << "无效选择，请重新输入" << std::endl;
        }
    }
}
